﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.Design;
using System.Data;
using System.Data.Services.Client;
using System.Collections;

namespace ODataSource
{
    public class ODesignDataSourceView : DesignerDataSourceView
    {
        internal DataTable _data = null;
        internal object query;

        public ODesignDataSourceView(
            ODataSourceDesigner owner, string viewName)
            : base(owner, viewName)
        {
            var datasource = (this.DataSourceDesigner as ODataSourceDesigner).Control;
            var context = Activator.CreateInstance(datasource.View.ContextType);

            query = (context as DataServiceContext).GetType().GetProperty(datasource.View.EntitySetName).GetValue(context, null);
        }

        // Get data for design-time display
        public override IEnumerable GetDesignTimeData(
            int minimumRows, out bool isSampleData)
        {
            var s = Schema as ODataSourceViewSchema;
            var FieldCount = s.GetFields().Count();
            if (_data == null)
            {
                // Create a set of design-time fake data
                //_data = new ArrayList();
                //for (int i = 1; i <= minimumRows; i++)
                //{
                //    _data.Add(new BookItem("ID_" + i.ToString(),
                //        "Design-Time Title 0" + i.ToString()));
                //}
                //_data = (query as DataServiceQuery).Execute();
                //MessageBox.Show(_data.GetType().FullName);
                _data = new DataTable();

                foreach (var f in s.GetFields())
                    _data.Columns.Add(f.Name, f.DataType);

                for (var i = 0; i < minimumRows; i++)
                {
                    var a = new Object[FieldCount];
                    for (var j = 0; j < FieldCount; j++)
                        if (s.GetFields()[j].DataType == typeof(string))
                            a[j] = "abc";
                        else if (s.GetFields()[j].DataType == typeof(int))
                            a[j] = 123;
                        else if (s.GetFields()[j].DataType == typeof(Double))
                            a[j] = 123.456;
                        else
                            a[j] = s.GetFields()[j].DataType.ToString();
                    _data.LoadDataRow(a, true);
                }
            }

            isSampleData = true;
            return _data.AsDataView();
        }

        public override IDataSourceViewSchema Schema
        {
            get { return new ODataSourceViewSchema(this); }
        }

        // Allow getting the record count
        public override bool CanRetrieveTotalRowCount
        {
            get { return true; }
        }
        // Do not allow deletions
        public override bool CanDelete
        {
            get { return true; }
        }
        // Do not allow insertions
        public override bool CanInsert
        {
            get { return true; }
        }
        // Do not allow updates
        public override bool CanUpdate
        {
            get { return true; }
        }
        // Do not allow paging
        public override bool CanPage
        {
            get { return true; }
        }
        // Do not allow sorting
        public override bool CanSort
        {
            get { return true; }
        }
    }
}
